﻿namespace PMS.Client.FrmUser
{
    using PMS.BLL;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FrmSort : Form
    {
        private UserBLL bll = new UserBLL();
        private Button btnCancel;
        private Button btnSave;
        private IContainer components = null;
        private DataGridView dataGridView1;
        private DataTable dataTable = null;
        private Rectangle dragBoxFromMouseDown = Rectangle.Empty;
        private int indexOfItemUnderMouseOver = -1;
        private int indexOfItemUnderMouseToDrag = -1;
        private Label lbTitle;
        private int selectionIdx = 0;
        private DataGridViewTextBoxColumn UserID;
        private DataGridViewTextBoxColumn UserName;

        public FrmSort(int OrganID)
        {
            this.InitializeComponent();
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataTable = this.bll.GetNameList(OrganID);
            this.dataGridView1.DataSource = this.dataTable;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int userid = 0;
            foreach (DataGridViewRow row in (IEnumerable) this.dataGridView1.Rows)
            {
                userid = int.Parse(row.Cells["UserID"].Value.ToString());
                this.bll.UpdateSort(userid, row.Index + 1);
            }
            base.DialogResult = DialogResult.OK;
        }

        private void dataGridView1_DragOver(object sender, DragEventArgs e)
        {
            Point point = this.dataGridView1.PointToClient(new Point(e.X, e.Y));
            DataGridView.HitTestInfo info = this.dataGridView1.HitTest(point.X, point.Y);
            if ((info.Type != DataGridViewHitTestType.Cell) || (info.RowIndex == this.indexOfItemUnderMouseToDrag))
            {
                this.OnRowDragOver(-1);
            }
            else
            {
                this.OnRowDragOver(info.RowIndex);
            }
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            if (e.RowIndex == this.indexOfItemUnderMouseOver)
            {
                e.Graphics.FillRectangle(Brushes.Red, e.RowBounds.X, e.RowBounds.Y, e.RowBounds.Width, 2);
            }
        }

        private void dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                this.selectionIdx = e.RowIndex;
                this.Cursor = Cursors.Hand;
            }
        }

        private void dgv_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (((e.Clicks < 2) && (e.Button == MouseButtons.Left)) && (e.RowIndex > -1))
            {
                this.dataGridView1.DoDragDrop(this.dataGridView1.Rows[e.RowIndex], DragDropEffects.Move);
            }
        }

        private void dgv_DragDrop(object sender, DragEventArgs e)
        {
            int rowFromPoint = this.GetRowFromPoint(e.X, e.Y);
            if ((rowFromPoint >= 0) && e.Data.GetDataPresent(typeof(DataGridViewRow)))
            {
                DataGridViewRow data = (DataGridViewRow) e.Data.GetData(typeof(DataGridViewRow));
                DataRow row = ((DataRowView) data.DataBoundItem).Row;
                DataRow row3 = ((DataTable) this.dataGridView1.DataSource).NewRow();
                for (int i = 0; i < data.Cells.Count; i++)
                {
                    row3[i] = data.Cells[i].Value;
                }
                this.selectionIdx = rowFromPoint;
                ((DataTable) this.dataGridView1.DataSource).Rows.InsertAt(row3, rowFromPoint);
                ((DataTable) this.dataGridView1.DataSource).Rows.Remove(row);
                this.dataGridView1.ClearSelection();
                this.OnRowDragOver(-1);
                this.Cursor = Cursors.Default;
                this.dataGridView1.Rows[this.selectionIdx - 1].Selected = true;
            }
        }

        private void dgv_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void dgv_SelectionChanged(object sender, EventArgs e)
        {
            if ((((this.dataGridView1.Rows.Count > 0) && (this.selectionIdx > -1)) && (this.selectionIdx < (this.dataGridView1.Rows.Count - 1))) && (this.dataGridView1.Rows.Count <= this.selectionIdx))
            {
                this.selectionIdx = this.dataGridView1.Rows.Count - 1;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private int GetRowFromPoint(int x, int y)
        {
            for (int i = 0; i < this.dataGridView1.RowCount; i++)
            {
                Rectangle rowDisplayRectangle = this.dataGridView1.GetRowDisplayRectangle(i, false);
                if (this.dataGridView1.RectangleToScreen(rowDisplayRectangle).Contains(x, y))
                {
                    return i;
                }
            }
            return -1;
        }

        private void InitializeComponent()
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmSort));
            this.dataGridView1 = new DataGridView();
            this.btnCancel = new Button();
            this.btnSave = new Button();
            this.lbTitle = new Label();
            this.UserID = new DataGridViewTextBoxColumn();
            this.UserName = new DataGridViewTextBoxColumn();
            ((ISupportInitialize) this.dataGridView1).BeginInit();
            base.SuspendLayout();
            this.dataGridView1.AllowDrop = true;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.AllowUserToResizeColumns = false;
            this.dataGridView1.AllowUserToResizeRows = false;
            this.dataGridView1.BackgroundColor = Color.FromArgb(0xc2, 0xd9, 0xf7);
            this.dataGridView1.BorderStyle = BorderStyle.Fixed3D;
            this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new DataGridViewColumn[] { this.UserID, this.UserName });
            style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            style.BackColor = Color.FromArgb(0xc2, 0xd9, 0xf7);
            style.Font = new Font("宋体", 9f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
            style.ForeColor = SystemColors.ControlText;
            style.SelectionBackColor = SystemColors.Highlight;
            style.SelectionForeColor = SystemColors.HighlightText;
            style.WrapMode = DataGridViewTriState.False;
            this.dataGridView1.DefaultCellStyle = style;
            this.dataGridView1.Dock = DockStyle.Top;
            this.dataGridView1.EnableHeadersVisualStyles = false;
            this.dataGridView1.Location = new Point(0, 0x1f);
            this.dataGridView1.MultiSelect = false;
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.RowTemplate.Height = 0x17;
            this.dataGridView1.Size = new Size(0x133, 0x173);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.CellMouseDown += new DataGridViewCellMouseEventHandler(this.dgv_CellMouseDown);
            this.dataGridView1.CellMouseMove += new DataGridViewCellMouseEventHandler(this.dgv_CellMouseMove);
            this.dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(this.dataGridView1_RowPostPaint);
            this.dataGridView1.SelectionChanged += new EventHandler(this.dgv_SelectionChanged);
            this.dataGridView1.DragDrop += new DragEventHandler(this.dgv_DragDrop);
            this.dataGridView1.DragEnter += new DragEventHandler(this.dgv_DragEnter);
            this.dataGridView1.DragOver += new DragEventHandler(this.dataGridView1_DragOver);
            this.btnCancel.BackColor = Color.White;
            this.btnCancel.FlatStyle = FlatStyle.Popup;
            this.btnCancel.Location = new Point(0xb2, 0x19d);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 9;
            this.btnCancel.Text = "关闭";
            this.btnCancel.UseVisualStyleBackColor = false;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnSave.BackColor = Color.White;
            this.btnSave.FlatStyle = FlatStyle.Popup;
            this.btnSave.Location = new Point(70, 0x19d);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new Size(0x4b, 0x17);
            this.btnSave.TabIndex = 8;
            this.btnSave.Text = "保存";
            this.btnSave.UseVisualStyleBackColor = false;
            this.btnSave.Click += new EventHandler(this.btnSave_Click);
            this.lbTitle.BackColor = Color.Gold;
            this.lbTitle.Dock = DockStyle.Top;
            this.lbTitle.Font = new Font("宋体", 14.25f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.lbTitle.ForeColor = Color.Black;
            this.lbTitle.Location = new Point(0, 0);
            this.lbTitle.Name = "lbTitle";
            this.lbTitle.Size = new Size(0x133, 0x1f);
            this.lbTitle.TabIndex = 0x39;
            this.lbTitle.Text = "人员排序";
            this.lbTitle.TextAlign = ContentAlignment.MiddleCenter;
            this.UserID.DataPropertyName = "UserID";
            this.UserID.HeaderText = "ID";
            this.UserID.Name = "UserID";
            this.UserID.ReadOnly = true;
            this.UserID.Visible = false;
            this.UserName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.UserName.DataPropertyName = "UserName";
            this.UserName.HeaderText = "人员姓名";
            this.UserName.Name = "UserName";
            this.UserName.ReadOnly = true;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xc2, 0xd9, 0xf7);
            base.ClientSize = new Size(0x133, 0x1bb);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnSave);
            base.Controls.Add(this.dataGridView1);
            base.Controls.Add(this.lbTitle);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FrmSort";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "人员排序";
            ((ISupportInitialize) this.dataGridView1).EndInit();
            base.ResumeLayout(false);
        }

        private void OnRowDragOver(int rowIndex)
        {
            if (this.indexOfItemUnderMouseOver != rowIndex)
            {
                int indexOfItemUnderMouseOver = this.indexOfItemUnderMouseOver;
                this.indexOfItemUnderMouseOver = rowIndex;
                if (indexOfItemUnderMouseOver > -1)
                {
                    this.dataGridView1.InvalidateRow(indexOfItemUnderMouseOver);
                }
                if (rowIndex > -1)
                {
                    this.dataGridView1.InvalidateRow(rowIndex);
                }
            }
        }
    }
}

